package account.webapi;

import java.io.Serializable;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

class ResultError implements Result<Object> {

    private String status;

    private final Object data;

    private List<Serializable> detailList;

    @Override
    public String getStatus() {
        return status;
    }

    @Override
    public Object getData() {
        return data;
    }

    public List<Serializable> getDetailList() {
        return detailList;
    }

    private ResultError() {
        data = null;
    }

    public ResultError(AppErrorCode errorCode) {
        this();
        this.status = errorCode.toString();
        detailList = new ArrayList<>();
    }

    /**
     * 追加若干条错误信息字符串
     *
     * @param allErrorMessages 错误详情
     * @return 响应结构体
     */
    public ResultError withAdditionalErrorMsgList(Collection<? extends Serializable> allErrorMessages) {
        detailList.addAll(allErrorMessages);
        return this;
    }

    /**
     * 追加一条错误信息字符串
     *
     * @param msg 错误详情
     * @return 响应结构体
     */
    public ResultError withAdditionalErrorMsg(Serializable msg) {
        detailList.add(msg);
        return this;
    }

    /**
     * 时间戳格式，例如年-月-日 时:分:秒.毫秒 GMT+8
     */
    private static final DateTimeFormatter FMT =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS O", Locale.CHINA);

    /**
     * @return 时间戳字符串
     */
    public String getTimestamp() {
        final ZonedDateTime now = ZonedDateTime.now();
        return FMT.format(now);
    }
}
